#include "ProxClone.h"


GameApp::GameApp()
{
	Surf_Display = NULL;
    Running = true;
	mGameState=NULL;
	//mMouseUp=false;
	//mMouseDown=false;
	int choosedColor=Hexa::BLUE;
	//mThisGameTurn=choosedColor;
}

GameApp::~GameApp()
{
	//
}

bool GameApp::OnInit()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) < 0) 
	{
        return false;
    }

    if((Surf_Display = SDL_SetVideoMode(800, 680, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) 
	{
        return false;
    }
		if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0)
    {
        return false;
    }
	printf("Initializing TTF... ");
    if(TTF_Init() < 0)
    {
        fprintf(stderr, "Unable to initialize TTF: %s\n", SDL_GetError());
        exit(1);
	}
	SDL_ShowCursor(0);
		mHandCursor=new HandCursor(*Surf_Display,"resources\\images\\cursor01.png"
											,"resources\\images\\cursor02.png"
											,"resources\\images\\cursor03.png"
											,"resources\\images\\cursor04.png");

	//mClickEffect
	mClickEffect= new AnimatedObject();
	mClickEffect->setScreen(*Surf_Display);
	mClickEffect->setPosition(660,300);
	mClickEffect->setSpeed(4);
	mClickEffect->setVisibleWhenNotPlaying(false);
	//mClickEffect->setVisibleFrameWhenNotPlaying(4);

	SDL_Surface* temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\ceff_0.png",1,1,1);
	mClickEffect->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\ceff_1.png",1,1,1);
	mClickEffect->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\ceff_2.png",1,1,1);
	mClickEffect->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\ceff_3.png",1,1,1);
	mClickEffect->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\ceff_4.png",1,1,1);
	mClickEffect->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\ceff_5.png",1,1,1);
	mClickEffect->AddFrame(temp);
    return true;
}

void GameApp::OnCleanup()
{
	SDL_FreeSurface(Surf_Display);
    SDL_Quit();
}

void GameApp::OnEvent(SDL_Event *Event)
{
	if(Event->type == SDL_QUIT) 
	{
        Running = false;
    }	

	if(mGameState!=NULL)
	{
		mGameState->OnEvent(Event);
	}
	ChangeGameState();

	mHandCursor->OnEvent(Event);
	///
	switch (Event->type)
    {
      case SDL_MOUSEMOTION:
		  {
				mCursorXValue=static_cast<float>(Event->motion.x);
				mCursorYValue=static_cast<float>(Event->motion.y);
				
		  }
		  break;
      case SDL_MOUSEBUTTONDOWN:
		  {
			//mMouseDown=true;
			mClickEffect->setPosition(mCursorXValue-60,mCursorYValue-60);
			mClickEffect->setPlaying();
		  }
		  break;
      case SDL_MOUSEBUTTONUP:
		  {
			  std::cout<<" ColoredBlue: "<<HexaTile::ColoredBlue<<std::endl;
			  std::cout<<" ColoredGreen: "<<HexaTile::ColoredGreen<<std::endl;
			 // if(mMouseDown)
			//{
			//	mMouseUp=true;
			//}
		  }
		  break;
	}
}

void GameApp::OnRender()
{
	if(!Running)
	{
		return;
	}

	if(mGameState!=NULL)
	{
		mGameState->OnRender(Surf_Display);
	}

	mClickEffect->Render();
	if(Game::StateManager::GetInstance().GetState()!=Game::State::INTRO)
	{
		mHandCursor->Render();
	}
	SDL_Flip(Surf_Display);

}

void GameApp::ChangeGameState()
{
	if(!(Game::StateManager::GetInstance().IsStateChanging()))
	{
		return;
	}
	else
	{
		Game::StateManager::GetInstance().HoldState();
		if(mGameState!=NULL)
		{
			mGameState->OnCleanup();
			delete mGameState;//delete previous state
		}
		switch(Game::StateManager::GetInstance().GetState())
		{
		    case Game::State::INTRO :
				{
					mGameState=new SplashState();
					mGameState->OnInit(Surf_Display);
					break;
				}
			case Game::State::MAINMENU :
				{
					mGameState=new MainMenuState();
					mGameState->OnInit(Surf_Display);
					break;
				}
			case Game::State::PLAYING :
				{
					mGameState=new PlayState();
					mGameState->OnInit(Surf_Display);
					break;
				}
			case Game::State::QUIT : 
				{
					Running=false;
					break;
				}
		}

	}
}

void GameApp::OnLoop()
{
	if(mGameState!=NULL && Running)
	{
		mGameState->OnLoop();
	}
	ChangeGameState();
}

int GameApp::OnExecute()
{
	if(OnInit() == false) {
        return -1;
    }
	Game::StateManager::GetInstance().SetState(Game::State::INTRO);
	ChangeGameState();
    SDL_Event Event;
    while(Running) {
        while(SDL_PollEvent(&Event)) 
		{
			//Game::GameGUI::GetInstance().InjectInputToGUI(mRunning,&Event);
			OnEvent(&Event);
        }
        OnLoop();
        OnRender();
    }

    OnCleanup();
    return 0;
}






